﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.student
{
    public partial class view_correction : System.Web.UI.Page
    {
        int assignment_id;
        int user_id;
        int class_id;

        protected void Page_Load(object sender, EventArgs e)
        {

            assignment_id = Convert.ToInt32(Session["assignment_id"]);
            user_id = Convert.ToInt32(Session["user_id"]);
            class_id = Convert.ToInt32(Session["class_id"]);
            populatePage();
        }

        private void populatePage()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var assignment = (from a in db.assignments
                                where a.assignment_id == assignment_id
                                select a).SingleOrDefault();

            Label1.Text = assignment.assignment_title;
            Label2.Text = assignment.assignment_description;
            Label3.Text = assignment.assignment_deadline.ToString();

            var submission = (from s in db.submissions
                              join f in db.files on s.file_id equals f.file_id
                              where s.assignment_id == assignment_id
                              where s.user_id == user_id
                              select new 
                              {
                                id = s.submission_id,
                                date = s.submission_date,
                                file_id = f.file_id,
                                file_name = f.file_name,
                                file_type = f.file_type,
                                file_size = f.file_size,
                                file_content = f.file_content
                              }).SingleOrDefault();

            if (submission != null)
            {
                Label5.Text = submission.date.ToString();
                Label6.Text = "<a href='../downloadFile.aspx?id=" + submission.file_id +"'>" + submission.file_name + "</a>";

                var correction = (from c in db.corrected_submission
                                  where c.submission_id == submission.id
                                  select c).SingleOrDefault();

                if (correction != null)
                {
                    Label7.Text = "Rættað";
                    Label8.Text = correction.corrected_submission_grade;
                    if (correction.corrected_submission_approved == 1)
                    {
                        Label9.Text = "Góðtikið";
                        Label9.ForeColor = System.Drawing.Color.Green;
                    }
                    else
                    {
                        Label9.Text = "Ikki Góðtikið";
                        Label9.ForeColor = System.Drawing.Color.Red;
                    }
                    Label10.Text = correction.corrected_submission_comment; 
                }
                else
                {
                    Label7.Text = "Ikki rættað";
                }

            }
            else
            {
                Label5.Text = "Ikki latið inn";
                Label6.Text = "Ikki latið inn"; 
                Label7.Text = "Ikki latið inn";
                Label8.Text = "Ikki latið inn";
                Label9.Text = "Ikki latið inn";
                Label10.Text = "Ikki latið inn";
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("viewClass.aspx?id=" + class_id);
        }
    }
}